/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.pojo.Point;
import com.iti.jets.gpts.pojo.Terminal;
import com.iti.jets.gpts.pojo.TranService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * Jersey REST client generated for REST resource:PointService
 * [/pointservice]<br>
 * USAGE:
 * <pre>
 *        PointService client = new PointService();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author khalifaabdelaziz
 */
public class PointService {

    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:48651/GPTRP/rest";

    public PointService() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("pointservice");
    }

    public List<Terminal> getAllpoints() throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path("getallpoints");
        JSONArray get = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<Terminal> points = new ArrayList<Terminal>();
        for (int i = 0; i < get.length(); i++) {
            JSONObject jsonPoint = get.getJSONObject(i);
            Point point = new Point();
            point.setId(jsonPoint.getInt("id"));
            point.setLocation(jsonPoint.getString("location"));
            point.setName(jsonPoint.getString("name"));
            point.setIsStop(jsonPoint.getBoolean("is_stop"));
            int aInt = jsonPoint.getInt("terminal");
 
            Terminal terminal = new Terminal(point);
            terminal.setIdPoint(aInt);
          
            points.add(terminal);
        }
        return points;
    }

    public Point addpoint(Point point,boolean terminal) throws UniformInterfaceException, JSONException {
        JSONObject jsonPoint = new JSONObject();

        jsonPoint.put("name", point.getName());
        jsonPoint.put("location", point.getLocation());
        jsonPoint.put("is_stop", point.getIsStop());
        jsonPoint.put("is_terminal", terminal);
        
        System.out.println(jsonPoint.toString());
        
        JSONObject post = webResource.path("addpoint").type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(JSONObject.class, jsonPoint);
        
        Point presPoint = new Point();
        presPoint.setId(post.getInt("id"));
        presPoint.setIsStop(post.getBoolean("is_stop"));
        presPoint.setLocation(post.getString("location"));
        presPoint.setName(post.getString("name"));
        System.out.println("client ============   "+presPoint.toString());
        return presPoint;
    }

    public void close() {
        client.destroy();
    }

}
